package net.wanho.cloudconsumer7001.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import net.wanho.cloudconsumer7001.client.UserClient;
import net.wanho.cloudconsumer7001.exception.BlockExceptionUtil;
import net.wanho.entity.User;
import net.wanho.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {

    //@Resource
    //private RestTemplate restTemplate;
    @Resource
    private UserClient userClient;


    @SentinelResource(value = "test1",blockHandlerClass = BlockExceptionUtil.class,blockHandler = "handlerException")
    @GetMapping("/test")
    public String test() {
        //String str = restTemplate.getForObject("http://localhost:6001/user/test", String.class);
        String str = userClient.test();

        return "consumer:"+str;
    }
    @SentinelResource(value = "findByName",blockHandlerClass = BlockExceptionUtil.class,blockHandler = "handlerException")
    @GetMapping("/findByName")
    public AjaxResult findByName(String name) {
        //String str = restTemplate.getForObject("http://localhost:6001/user/findByName?name="+name, String.class);
        return userClient.findByName(name);
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        return userClient.deleteById(id);
    }

    @PostMapping
    public AjaxResult addUser(@RequestBody User user){
        return userClient.addUser(user);
    }

    @PutMapping
    public AjaxResult updateUser(@RequestBody User user){
        return userClient.updateUser(user);
    }
    @SentinelResource(value = "query",blockHandlerClass = BlockExceptionUtil.class,blockHandler = "handlerException")
    @GetMapping("/query")
    public AjaxResult queryUser(User user){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",user.getName());
        map.put("age",user.getAge());
        map.put("id",user.getId());
        return userClient.queryUser(map);
    }


}
